//
//  MainController.m
//  xKey Interface
//
//  Created by Guillaume SOURDRILLE on 19/02/12.
//  Copyright (c) 2012 Edenyorke. All rights reserved.
//

#import "MainController.h"
#import "MessageUtils.h"
#import "SettingsView.h"


@implementation MainController
@synthesize listeJeuxController,settingsViewController, messageUtils,settingsViewControllerIpad, infosViewController, infosViewControllerIpad;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self initParameters];
    
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    listeJeuxButton = nil;
    listeJeuxController = nil;
    settingsViewController = nil;
    messageUtils = nil;
    version = nil;
    settingsButton = nil;
    donateButton = nil;
    paypalMessage = nil;
    gamerTag = nil;
    infoButton = nil;
    infosViewController = nil;
    [super viewDidUnload];

}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}



- (void)initParameters{
    messageUtils =  [[MessageUtils alloc] init];
    [messageUtils initData];
    NSString *versionValue  = [messageUtils getValue:@"version"];
    version.text = versionValue;
    UIImage *image = [UIImage imageNamed:@"xkey_logo.png"];
    imageView.image = image;
    paypalMessage.text = NSLocalizedString(@"paypal.message", @"");
    [listeJeuxButton setTitle:NSLocalizedString(@"main.games.button", @"") forState:UIControlStateNormal];
    [infoButton setTitle:NSLocalizedString(@"main.infos.button", @"") forState:UIControlStateNormal];
    [settingsButton setTitle:NSLocalizedString(@"main.settings.button", @"") forState:UIControlStateNormal];

       
}



- (void)initButton{
   
    ipXkey = [messageUtils getValue:@"ipxkey"];
    if (ipXkey == nil || [ipXkey isEqualToString:@""]){
        [listeJeuxButton setEnabled:NO];
        [infoButton setEnabled:NO];
    }else{
        [listeJeuxButton setEnabled:YES];
        [infoButton setEnabled:YES];
    }
       }

- (void)initGamerTag{
    
    NSString *gamerTagId = [messageUtils getValue:@"gamertag"];
    if (gamerTagId != nil && ![gamerTagId isEqualToString:@""]){
        NSString *path = [NSString stringWithFormat:@"%@/%@/%@", @"http://www.xboxgamertag.com/gamercard/",gamerTagId, @"/newnxe/card.png"];
        NSURL *url = [NSURL URLWithString:path];
        NSData *data = [NSData dataWithContentsOfURL:url];
        UIImage *img = [[UIImage alloc] initWithData:data ];
        gamerTag.image = img;
    }else{
        gamerTag.image = nil;
    }
   
}

- (IBAction)viewListeJeux {
    
    listeJeuxController = [[ListeJeuxController alloc] init];
    listeJeuxController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:listeJeuxController];
    navController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    [self presentModalViewController:navController animated:YES];
      
}

- (IBAction)viewInfos:(id)sender {
    
    infosViewController = [[InfosViewController alloc] init];
    infosViewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:infosViewController];
    navController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    [self presentModalViewController:navController animated:YES];

    
}

- (IBAction)openDonate {
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString: @"https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=RLW9M84QY66H6&lc=US&item_name=Edenyorke&item_number=xkeyInterface&currency_code=EUR&bn=PP%2dDonationsBF%3abtn_donate_LG%2egif%3aNonHosted"]];
}

- (IBAction)viewSettings {
    
    settingsViewController = [[SettingsView alloc] initWithParam:ipXkey];
    settingsViewController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    [self presentModalViewController:settingsViewController animated:YES];
    
}

- (IBAction)viewSettingsIpad:(id)sender {
    ipXkey = [messageUtils getValue:@"ipxkey"];
    settingsViewControllerIpad = [[SettingsPopover alloc] initWithParam:ipXkey];
    if (popoverController==nil || settingsViewControllerIpad.popoverController == nil) {
        popoverController=[[UIPopoverController alloc]
                           initWithContentViewController:settingsViewControllerIpad];
        [popoverController presentPopoverFromRect:settingsButton.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];    
        popoverController.delegate=self;
        settingsViewControllerIpad.popoverController = popoverController;
    }
}

- (IBAction)viewInfosIpad:(id)sender {
    ipXkey = [messageUtils getValue:@"ipxkey"];
    infosViewControllerIpad = [InfosPopover alloc];
    if (popoverController==nil || infosViewControllerIpad.popoverController == nil) {
        popoverController=[[UIPopoverController alloc]
                           initWithContentViewController:infosViewControllerIpad];
        [popoverController presentPopoverFromRect:infoButton.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
        popoverController.delegate=self;
        infosViewControllerIpad.popoverController = popoverController;
    }
}

- (void)viewWillAppear:(BOOL)animated {
    [self initButton];
    [self initGamerTag];
}

-(void)popoverControllerDidDismissPopover:(UIPopoverController *)controller {
            
    popoverController=nil;
    [self initButton];
    [self initGamerTag];
}

@end
